window.addEventListener('DOMContentLoaded' ,function(){
    var goback = document.querySelector('#goback');
    var navFirst = document.querySelector('.nav-first');
    var html = document.documentElement;
    var body = document.body;

    var navTop = navFirst.offsetTop;
    

    //屏幕滑动事件
    window.addEventListener('scroll', display);
    function display(){
        if (window.pageYOffset >= navTop){
            goback.style.display = 'block';
        } else {
            goback.style.display = 'none';
        }
    }

    //goback点击事件--返回顶部（动画）
    goback.addEventListener('click', function (){
        animal(window,0);
        
    });


    function animal(obj, target, callback){
        clearInterval(obj.timer)
        obj.timer = setInterval(moveRight, 15);
        function moveRight(){
            var top = obj.pageYOffset;
            var step = (target - top) / 10; // 可以通过步长修改移动的速度快慢（缓动动画）
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            if (top == target){
                clearInterval(obj.timer);
                callback && callback();
            } else {
                obj.scroll(0, top + step);
                display();
            }
        }
    }
    
})